import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useSettingsStore = defineStore('settings', () => {
  const fontSizeOptions = [
    { label: '小', titleSize: '16px', fontSize: '12px' },
    { label: '中', titleSize: '18px', fontSize: '14px' },
    { label: '大', titleSize: '20px', fontSize: '16px' },
    { label: '特大', titleSize: '22px', fontSize: '18px' },
  ]

  const fontSize = ref('14px') // 正文字体
  const titleSize = ref('18px') // 标题字体

  const themeOptions = [
    { label: '绿色', themeColor: '#009f45', themeLightColor: '#ceedcc' },
    { label: '海蓝', themeColor: '#2386ee', themeLightColor: '#b2e1ef' },
    { label: '橙色', themeColor: '#fd7b3c', themeLightColor: '#ffd8bc' },
    { label: '灰绿', themeColor: '#7da190', themeLightColor: '#def5eb' },
    { label: '红色', themeColor: '#de3a25', themeLightColor: '#ffbebe' }
  ]

  const themeColor = ref('#2386ee')
  const themeLightColor = ref('#b2e1ef')

  const setFontSize = (index: number) => {
    const selected = fontSizeOptions[index]
    fontSize.value = selected.fontSize
    titleSize.value = selected.titleSize
  }

  const setTheme = (index: number) => {
    const selected = themeOptions[index]
    themeColor.value = selected.themeColor
    themeLightColor.value = selected.themeLightColor
  }

  const initFromStorage = () => {
    fontSize.value = uni.getStorageSync('fontSize') || fontSize.value
    titleSize.value = uni.getStorageSync('titleSize') || titleSize.value
    themeColor.value = uni.getStorageSync('themeColor') || themeColor.value
    themeLightColor.value = uni.getStorageSync('themeLightColor') || themeLightColor.value
  }

  const saveToStorage = () => {
    uni.setStorageSync('fontSize', fontSize.value)
    uni.setStorageSync('titleSize', titleSize.value)
    uni.setStorageSync('themeColor', themeColor.value)
    uni.setStorageSync('themeLightColor', themeLightColor.value)
  }

  initFromStorage()

  return {
    fontSize,
    titleSize,
    themeColor,
    themeLightColor,
    fontSizeOptions,
    themeOptions,
    setFontSize,
    setTheme,
    saveToStorage,
    initFromStorage,
  }
})
